package com.quectel.mobile.controller.parking;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import com.google.common.base.Preconditions;
import com.quectel.constant.core.parkingcard.ParkingCardConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.pay.PayBusinessType;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.parking.dto.ParkingOrderDto;
import com.quectel.core.module.parkingcard.dto.ParkingCardDto;
import com.quectel.core.module.parkingcard.dto.ParkingCardOrderDto;
import com.quectel.core.module.parkingcard.service.ParkingCardOrderService;
import com.quectel.core.module.parkingcard.service.ParkingCardService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.interceptor.LoginMobileUserSession;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.util.common.DateUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("parkingCard")
@Tag(name = "ParkingCardController", description = "停车月卡相关api")
public class ParkingCardController extends BaseController {

    @DubboReference
    private ParkingCardService parkingCardService;
    @DubboReference
    private ParkingCardOrderService parkingCardOrderService;

    /**
     * 月卡账单 根据车辆查询月卡账单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("parkingCardList")
    @Operation(summary = "月卡账单")
    public Response<Page<ParkingCardOrderDto>> parkingCardList(
            @Parameter(description = "从1开始的页码数", required = true) @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条", required = true) @RequestParam Integer pageSize,
            @Parameter(description = "车牌号") @RequestParam(required = false) String license,
            @Parameter(description = "车牌颜色") @RequestParam(required = false) Byte licenseColor,
            @Parameter(description = "月份筛选 yyyy-MM") @RequestParam(required = false) String monthStr,
            @Parameter(description = "支付状态 0未缴费 1已缴费 2已退款") @RequestParam(required = false) Byte payStatus,
            @Parameter(description = "开票状态 0未提交开票 1申请开票(开票中) 2已开票 3已红冲 4已关闭") @RequestParam(required = false) Byte invoiceStatus,
            @Parameter(description = "审核状态 1待审核 2已审核 3驳回") @RequestParam(required = false) Byte auditStatus
    ) {
        SessionHolder.checkHousehold();

        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        LoginMobileUserSession loginMobileUserSession = SessionHolder.getLoginMobileUserSession();
        Preconditions.checkNotNull(loginMobileUserSession);

        HouseholdDto currentHousehold = loginMobileUserSession.getCurrentHousehold();
        Preconditions.checkNotNull(currentHousehold);

        request.put(LambdaUtil.getFieldName(ParkingOrderDto::getHouseholdId), currentHousehold.getId());
        if (StringUtils.isNotBlank(monthStr)) {
            Date date = DateUtils.parse(monthStr, DateUtils.FormatType.MONTH);
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getLeft(), DateUtils.format(DateUtil.beginOfMonth(date), DateUtils.FormatType.COMMON));
            request.put(SystemConstants.TIME_SLOT_KEY_PAIR.getRight(), DateUtils.format(DateUtil.endOfMonth(date), DateUtils.FormatType.COMMON));
        }

        List<ParkingCardOrderDto> list = null;
        int total = parkingCardOrderService.queryTotal(request.getParams());
        if (total > 0) {
            list = parkingCardOrderService.queryList(request.getParams());
        } else {
            list = Lists.newArrayList();
        }
        Page<ParkingCardOrderDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<ParkingCardOrderDto>>ok().wrap(page);
    }

    /**
     * 月卡列表
     *
     * @return
     */
    @GetMapping("cards")
    @Operation(summary = "月卡列表")
    public Response<List<ParkingCardDto>> cards() {
        SessionHolder.checkHousehold();
        HouseholdDto currentHousehold = Objects.requireNonNull(SessionHolder.getLoginMobileUserSession()).getCurrentHousehold();

        List<ParkingCardDto> list = parkingCardService.selectByVillageId(currentHousehold.getVillageId());
        List<ParkingCardDto> filtedList = list.parallelStream()
                .filter(o -> ParkingCardConstants.CardStatusEnum.ENABLE.getStatus().equals(o.getStatus()))
                .collect(Collectors.toList());
        filtedList.forEach(o -> o.setBusinessType(PayBusinessType.PARKING_CARD_FEE));
        return Response.<List<ParkingCardDto>>ok().wrap(filtedList);
    }


}
